"""应用配置管理"""

import os
from dataclasses import dataclass
from typing import Optional
from dotenv import load_dotenv

# 加载环境变量
load_dotenv()


@dataclass
class DatabaseConfig:
    """数据库配置"""
    url: str = "sqlite:///./cairo_chatbot.db"


@dataclass
class LLMConfig:
    """LLM配置"""
    provider: str = os.getenv("LLM_PROVIDER", "qwen")
    openai_model: str = os.getenv("OPENAI_MODEL", "gpt-4o-mini")
    anthropic_model: str = os.getenv("ANTHROPIC_MODEL", "claude-3-5-sonnet-latest")
    qwen_model: str = os.getenv("QWEN_MODEL", "qwen-plus")
    openai_api_key: Optional[str] = os.getenv("OPENAI_API_KEY")
    anthropic_api_key: Optional[str] = os.getenv("ANTHROPIC_API_KEY")
    dashscope_api_key: Optional[str] = os.getenv("DASHSCOPE_API_KEY")


@dataclass
class APIConfig:
    """API配置"""
    host: str = os.getenv("API_HOST", "0.0.0.0")
    port: int = int(os.getenv("API_PORT", "8000"))
    debug: bool = os.getenv("DEBUG", "false").lower() == "true"
    cors_origins: list = None
    
    def __post_init__(self):
        if self.cors_origins is None:
            self.cors_origins = ["*"]


@dataclass
class UIConfig:
    """UI配置"""
    host: str = os.getenv("UI_HOST", "localhost")
    port: int = int(os.getenv("UI_PORT", "8501"))
    theme: str = os.getenv("UI_THEME", "light")


@dataclass
class ExternalToolsConfig:
    """外部工具配置"""
    github_token: Optional[str] = os.getenv("GITHUB_TOKEN")
    enable_external_search: bool = os.getenv("ENABLE_EXTERNAL_SEARCH", "true").lower() == "true"
    max_external_results: int = int(os.getenv("MAX_EXTERNAL_RESULTS", "5"))
    search_timeout: int = int(os.getenv("SEARCH_TIMEOUT", "10"))
    cache_ttl: int = int(os.getenv("CACHE_TTL", "3600"))  # 1小时
    
    # 文档基础URL
    cairo_docs_base: str = "https://www.starknet.io/cairo-book/"
    starknet_docs_base: str = "https://docs.starknet.io"
    
    # GitHub搜索配置
    github_repos: list = None
    
    # Stack Overflow配置
    stackoverflow_tags: list = None
    
    def __post_init__(self):
        if self.github_repos is None:
            self.github_repos = [
                "starkware-libs/cairo",
                "starkware-libs/starknet-foundry",
                "OpenZeppelin/cairo-contracts",
                "keep-starknet-strange/alexandria"
            ]
        
        if self.stackoverflow_tags is None:
            self.stackoverflow_tags = ["cairo", "starknet", "cairo-lang"]


@dataclass
class AppConfig:
    """应用总配置"""
    database: DatabaseConfig = None
    llm: LLMConfig = None
    api: APIConfig = None
    ui: UIConfig = None
    external_tools: ExternalToolsConfig = None
    
    # 应用元信息
    app_name: str = "Cairo 智能合约助手"
    app_version: str = "1.0.0"
    app_description: str = "基于 LangGraph 的 Cairo 垂直领域聊天机器人"

    def __post_init__(self):
        if self.database is None:
            self.database = DatabaseConfig()
        if self.llm is None:
            self.llm = LLMConfig()
        if self.api is None:
            self.api = APIConfig()
        if self.ui is None:
            self.ui = UIConfig()
        if self.external_tools is None:
            self.external_tools = ExternalToolsConfig()


# 全局配置实例
config = AppConfig()
